package com.ztz.mynewdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

/**
 * Created by wqewqe on 2017/12/18.
 */

public class ViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    public ViewHolder(View itemView) {
        super(itemView);
        mViews=new SparseArray<>();
    }
    public <T extends View>T getView(int viewId)
    {
        View view=mViews.get(viewId);
        if(view==null)
        {
            view=itemView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }
    public ViewHolder setText(int viewId,CharSequence text)
    {
        TextView view=getView(viewId);
        view.setText(text);
        return this;
    }
    public ViewHolder setViewVisibility(int viewId,int visibility)
    {
        getView(viewId).setVisibility(visibility);
        return this;
    }
    public ViewHolder setOnClickListener(int viewId,View.OnClickListener onClickListener){
        getView(viewId).setOnClickListener(onClickListener);
        return this;
    }
    public void setOnItemClickListener(View.OnClickListener onItemClickListener)
    {
        itemView.setOnClickListener(onItemClickListener);
    }
    public ViewHolder setImageUrl(int viewId,String resourceId)
    {
        SmartImageView smartImageView=getView(viewId);
        smartImageView.setImageUrl(resourceId);
//        ImageView view=getView(viewId);
//        if (imageLoader == null) {
//            throw new NullPointerException("imageLoader is null!");
//        }
//        imageLoader.displayImage(view.getContext(),view,imageLoader.getmImagePath());
        return this;
    }
    public static abstract class HolderImageLoader
    {
        private String mImagePath;

        public HolderImageLoader(String mImagePath) {
            this.mImagePath = mImagePath;
        }

        public String getmImagePath() {
            return mImagePath;
        }
        public abstract void displayImage(Context context,ImageView imageView,String imagePath);
    }
}
